import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 
 * @author PUSS1401
 * 
 */
@WebServlet("/Statistics")
public class servletStatistics extends servletBase {
	private static final long serialVersionUID = 1L;
	private final int START = 0;
	private final int PROJECT = 2;
	private final int USER = 3;
	private final int ROLE = 1;
	private int state = 0;
	private String[] rolelist = { "Projektledare", "Utvecklare",
			"Systemansvarig", "Testare" };
	private HtmlGenerator gen = new HtmlGenerator();
	/**
	 * A servlet that handles the statistics page. Extends servletBase.
	 * First it is checked that an user is logged in and has chosen a project, otherwise the user is redirected to the appropriate page.
	 * 
	 * A regular member of a project, a member that has another role than "Projektledare" is shown a summary of its timereports.
	 * A member with the role "Projektledare" is given the choice to see statistics for all members, one by one or all together, or by role.
	 * 
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		if (session == null) {
			response.sendRedirect("lostSession.html");
			return;
		}

		if (request.getQueryString() != null
				&& request.getQueryString().contains("project")) {
			System.out.println(request.getQueryString());
			System.out.println("state 1");
			state = PROJECT;
		}

		else if (request.getQueryString() != null
				&& request.getQueryString().indexOf(("user")) >= 0) {
			System.out.println(request.getQueryString());
			state = USER;
			System.out.println("state 2");
		} else if (request.getQueryString() != null
				&& request.getQueryString().indexOf(("role")) >= 0) {
			System.out.println(request.getQueryString());
			state = ROLE;
			System.out.println("state 3");
		}

		else if (request.getQueryString() == null) {
			state = START;
		}

		if (!loggedIn(request)) {
			response.sendRedirect("LogIn");
		} else {
			if ((session.getAttribute("project").equals("noproject"))) {
				response.sendRedirect("Projects");
			} else {

				response.setContentType("text/html");
				PrintWriter out = response.getWriter();

				String name = session.getAttribute("name").toString();
				String project = session.getAttribute("project").toString();
				String role = session.getAttribute("role").toString();

				out.print(gen.getTop(name, project, role));
				out.print(gen.getProjMenu());

				if (role.equals("Projektledare")) {

					out.print("Select the statistics you want to see <br>");
					String html = "<form action=" + formElement("Statistics") + "method="
					+ formElement("get") + '>';
					out.print(html+gen.chooseAlternative());

					if (state == PROJECT) {
						out.print(gen.viewTimeReport(db.sumTRAllWeek(project)));
					} else if (state == USER) {
						out.println(gen.selectMember(project,
								db.listMembers(project)));
						if (request.getQueryString().contains("ChosenUser")) {
							String members[] = db.listMembers(project);
							for (int i = 0; i < members.length; i++) {
								System.out.println(request.getQueryString());
								System.out
										.println(members[i].replace(" ", "+"));
								if (request.getQueryString().equals(
										"ChosenUser="
												+ members[i].replace(" ", "+"))) {
									out.println(gen.viewTimeReport(db
											.sumAllTRsUser(members[i], project)));
								}
							}
						}

					} else if (state == ROLE) {
						out.println(gen.selectRole(rolelist));
						if (request.getQueryString().contains("ChosenRole")) {
							for (int i = 0; i < rolelist.length; i++) {
								System.out.println(request.getQueryString());
								System.out.println(rolelist[i]
										.replace(" ", "+"));
								if (request
										.getQueryString()
										.equals("ChosenRole="
												+ rolelist[i].replace(" ", "+"))) {
									out.println(gen.viewTimeReport(db
											.sumTRRole(project, rolelist[i])));
								}
							}
						}
					}
				} else {
					out.print(gen.viewTimeReport(db
							.sumAllTRsUser(name, project)));
				}
				out.print(gen.getBot());
				out.close();
			}
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}
}